package com.example.multipleThread;

import java.util.concurrent.Semaphore;

/**
 * 多线程打印
 */
public class ThreadPrint {
    private Semaphore two = new Semaphore(0);
    private Semaphore three = new Semaphore(0);


    public void first() throws InterruptedException {

        // printFirst.run() outputs "first". Do not change or remove this line.
        // printFirst.run();
        System.out.println(1);
        two.release();
    }

    public void second() throws InterruptedException {
        two.acquire();
        // printSecond.run() outputs "second". Do not change or remove this line.
        //printSecond.run();
        System.out.println(2);
        three.release();
    }

    public void third() throws InterruptedException {
        three.acquire();
        System.out.println(3);
        // printThird.run() outputs "third". Do not change or remove this line.
        // printThird.run();
    }


    static class Print {
        public static void main(String[] args) {
            ThreadPrint thread = new ThreadPrint();
            new Thread(() -> {
                try {
                    thread.third();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }).start();
            new Thread(() -> {
                try {
                    thread.second();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
            new Thread(() -> {
                try {
                    thread.first();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();


        }
    }
}

